var port = process.argv[2] || 8080;

var express = require('express');

var demo = require('./demo');


exports.start = function start() {
    var app = express();
    exports.http = require('http').createServer(app);

    app.set('view engine', 'jade');
    app.set('views', __dirname + '/views');
    app.set('view options', { layout: false });

    //change this favicon because you have no rights to it...
    app.use(express.favicon(__dirname + '/favicon.ico'));
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(require('express-validator'));

    //when NODE_ENV=production node index.js
    app.configure('production', function() {
        app.enable('view cache');
    });
    //else dev mode
    app.configure('development', function() {
        
    });

    //give all the locations of static content
    app.configure(function() {
        app.use('/js', express.static(__dirname + '/js'));
        app.use('/demo/assets', express.static(__dirname + '/demo/assets'));
        app.use('/demo/assets/image', express.static(__dirname + '/demo/assets/image'));
        app.use('/demo/assets/image/cursor', express.static(__dirname + '/demo/assets/image/cursor'));
        app.use('/stack/content', express.static(__dirname + '/stack/content'));
    });
    
    app.get('/', function(req, res) {
        res.render('index', {
            doll: {
                version: "0.0.1"
            }});
    });

    //demo routes
    app.use('/demo', require('./demo'));

    //the stack
    app.use('/stack', require('./stack'));

    //testing
    app.use('/net_test', express.static(__dirname + '/net_test'));
    app.use('/require_test', express.static(__dirname + '/require_test'));

    app.use(function(req,res,next) {
        res.writeHead(404);
        res.end(req.url + ' not found!');
    });

    app.use(function(err, req, res, next) {
        console.error(err.stack);
        res.send(500, 'Internal Server Error! Please tell me about this!');
    });

    exports.http.listen(port);
    console.log("doll.js Server\n => http://localhost:" + port);
}

